home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / vector.lha / vector / colorvector.c < prev    next >
C/C++ Source or Header  |  1991-11-23  |  2KB  |  122 lines

  1. #include "colorvector.h"
  2.  
  3. static inline float min(float a, float b) { return (a < b) ? a : b; }
  4.  
  5. static inline float max(float a, float b) { return (a > b) ? a : b; }
  6.  
  7. static inline float clamp(float v, float min, float max) {
  8.     return (v < min) ? min : (v > max) ? max : v;
  9. }
  10.  
  11. // Algorithms converted (with minor bug fixes) from Rogers
  12. static void hsv_to_rgb(
  13.     float  h, float  s, float  v,
  14.     float &r, float &g, float &b) {
  15.  
  16.     h = clamp(h, 0, 360);
  17.     s = clamp(s, 0, 1);
  18.     v = clamp(v, 0, 1);
  19.  
  20.     if (s == 0.0)
  21.     r = g = b = v;
  22.     else {
  23.     // Chromatic case
  24.     if (h < 0) h = 0;
  25.     else if (h > 360) h = 360;
  26.  
  27.     if (h == 360)
  28.         h = 0;
  29.  
  30.     h /= 60.0;
  31.     int i = int(floor(h));
  32.     float  f = h - i,
  33.            m = v * (1 - s),
  34.            n = v * (1 - s * f),
  35.            k = v * (1 - s * (1 - f));
  36.  
  37.     switch (i) {
  38.         case 0:
  39.         r = v;
  40.         g = k;
  41.         b = m;
  42.         break;
  43.         case 1:
  44.         r = n;
  45.         g = v;
  46.         b = m;
  47.         break;
  48.         case 2:
  49.         r = m;
  50.         g = v;
  51.         b = k;
  52.         break;
  53.         case 3:
  54.         r = m;
  55.         g = n;
  56.         b = v;
  57.         break;
  58.         case 4:
  59.         r = k;
  60.         g = m;
  61.         b = v;
  62.         break;
  63.         case 5:
  64.         r = v;
  65.         g = m;
  66.         b = n;
  67.         break;
  68.     }
  69.     }
  70. }
  71.  
  72. static void rgb_to_hsv(
  73.     float  r, float  g, float  b,
  74.     float &h, float &s, float &v) {
  75.  
  76.     r = clamp(r, 0, 1);
  77.     g = clamp(g, 0, 1);
  78.     b = clamp(b, 0, 1);
  79.  
  80.     // Value
  81.     v = max(r, max(g, b));
  82.  
  83.     // Saturation
  84.     float temp = min(r, min(g, b));
  85.  
  86.     if (v == 0.0)
  87.     s = 0;
  88.     else
  89.     s = (v - temp) / v;
  90.  
  91.     // Hue
  92.     if (s == 0.0)
  93.     h = 0;    // Undefined
  94.     else {
  95.     float Cr = (v - r) / (v - temp),
  96.           Cg = (v - g) / (v - temp),
  97.           Cb = (v - b) / (v - temp);
  98.  
  99.     if (r == v)        // Between yellow & magenta
  100.         h = Cb - Cg;
  101.     else if (g == v)    // Between cyan & yellow
  102.         h = 2 + Cr - Cb;
  103.     else // if (b == v) // Between magenta & cyan
  104.         h = 4 + Cg - Cr;
  105.  
  106.     h *= 60;
  107.  
  108.     if (h < 0)
  109.         h += 360;
  110.     }
  111. }
  112.  
  113. RGBcolor &RGBcolor::operator=(HSVcolor &c) {
  114.     hsv_to_rgb(c(0), c(1), c(2), (*this)[0], (*this)[1], (*this)[2]);
  115.     return *this;
  116. }
  117.  
  118. HSVcolor &HSVcolor::operator=(RGBcolor &c) {
  119.     rgb_to_hsv(c(0), c(1), c(2), (*this)[0], (*this)[1], (*this)[2]);
  120.     return *this;
  121. };
  122.